package com.trt.eatmeat.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by mayufeng on  2018/2/23
 * 时间处理类
 * @author mayufeng
 * @version 0.1
 */
public class DateUtils {
    public static final String formatDefault="yyyy-MM-dd";

    public static Date StringToDate(String date){
        if("NA".equals(date)){
            return null;
        }
        if("N/A".equals(date)){
            return null;
        }
        if("".equals(date)){
            return null;
        }
        SimpleDateFormat format = strToDateFormat(date);
        if(format==null){
            return null;
        }
        try {
            return  format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String dateToString(Date date){
        if(date==null){
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return  format.format(date);
    }

    public static String dateToString(Date date,String formatDate){
        if(date==null){
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(formatDate);
        return  format.format(date);
    }


    private static SimpleDateFormat strToDateFormat(String str){
        if(str.contains("/")){
            if(str.indexOf("/")==4){
                if (str.length()>18){
                    return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                }else if (str.length()>15){
                    return new SimpleDateFormat("yyyy/MM/dd HH:mm");
                }else if (str.length()>9){
                    return new SimpleDateFormat("yyyy/MM/dd");
                }else {
                    return new SimpleDateFormat("yyyy/MM/dd");
                }
            } else if(str.indexOf("/")==2){

                if (str.length()>18){
                    return new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                }else if (str.length()>15){
                    return new SimpleDateFormat("dd/MM/yyyy HH:mm");
                }else if (str.length()>9){
                    return new SimpleDateFormat("dd/MM/yyyy");
                }else {
                    return new SimpleDateFormat("dd/MM");
                }
            }

        }else if(str.contains("-")){
            if (str.length()>18){
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }else if (str.length()>15){
                return new SimpleDateFormat("yyyy-MM-dd HH:mm");
            }else if (str.length()>9){
                return new SimpleDateFormat("yyyy-MM-dd");
            }else {
                return new SimpleDateFormat("yyyy-MM-dd");
            }
        }else {
            return null;
        }
        return new SimpleDateFormat("yyyyMMdd");

    }


    public static boolean sameDate(Date d1, Date d2) {
        if(null == d1 || null == d2){
            return false;
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(d1);
        cal1.set(Calendar.HOUR_OF_DAY, 0);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(d2);
        cal2.set(Calendar.HOUR_OF_DAY, 0);
        cal2.set(Calendar.MINUTE, 0);
        cal2.set(Calendar.SECOND, 0);
        cal2.set(Calendar.MILLISECOND, 0);
        return  cal1.getTime().equals(cal2.getTime());
    }

    public static Date getLastMonthFristDay(){
        Calendar   cal_1=Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        cal_1.set(Calendar.HOUR_OF_DAY, 0);
        cal_1.set(Calendar.MINUTE, 0);
        cal_1.set(Calendar.SECOND, 0);
        return  cal_1.getTime();
    }

    public static Date getLastMonthEndDay(){
        Calendar   cal_1=Calendar.getInstance();//获取当前日期
        cal_1.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        cal_1.set(Calendar.HOUR_OF_DAY, 0);
        cal_1.set(Calendar.MINUTE, 0);
        cal_1.set(Calendar.SECOND, 0);
        return  cal_1.getTime();
    }




}
